<template>
  <div class="container">
    <!-- <h1>我是登录路由</h1> -->
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="loginFrom"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到蔚然后台管理系统</h2>
          <el-form-item prop="username">
            <el-input
              size="large"
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              size="large"
              :show-password="true"
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loadingStatus"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import userUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let userStore = userUserStore()
let $router = useRouter()
let loadingStatus = ref(false)
let loginForm = reactive({ username: 'admin', password: '111111' })
let loginForms = ref()
//自定义校验规则
const validatorUserName = (rule, value, callback) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少为五位'))
  }
}
const validatorPassWord = (rule, value, callback) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('账号长度至少为六位'))
  }
}
let rules = reactive({
  username: [
    // { required: true, message: '请输入用户名', trigger: 'blur' },
    // { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
    { trigger: 'blur', validator: validatorUserName },
  ],
  password: [{ trigger: 'blur', validator: validatorPassWord }],
})
const login = async () => {
  await loginForms.value.validate()
  loadingStatus.value = true
  try {
    //登录成功
    await userStore.userLogin(loginForm)
    $router.push('/')
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${getTime()}好！`,
    })
    loadingStatus.value = false
  } catch (error) {
    ElNotification({
      type: 'error',
      message: error.message,
    })
    loadingStatus.value = false
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .loginFrom {
    padding: 40px;
    position: relative;
    width: 70%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;

    h1 {
      font-size: 40px;
      color: white;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
